﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports log4net

Partial Class mtFundCenterCmmt
    Inherits System.Web.UI.Page

    Public AlertText, ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("mtFundCenterCmmt")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >>  Fund Center - Commitment Item"

        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            'WebGrid1.LayoutSettings.AllowAddNew = AddNew.No
            Session("SearchTxt") = ""
            'Session("FctCode") = ""
            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
        Else
            'PlanYearsTxt.Text = Session("PlanYearTxt")
        End If
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource

        'If IsPostBack = False Or Val(Session("PlanYearTxt")) = 0 Or Session("FctCode") = "" Or Session("SearchTxt") = "" Then
        '    Exit Sub
        'End If
        Dim adapter As New MasterDataSetTableAdapters.FundCenterCmmtsTableAdapter
        Dim dtTable As New MasterDataSet.FundCenterCmmtsDataTable

        Dim sql As String = "select  Id, FundCenter_FundCenterCode , FromCmmt_CmmtCode,ToCmmt_CmmtCode,planyear from FundCenterCmmts   "
        sql += " WHERE 1 = 1   "

        If fctrWebCombo.Value = "" And fctrWebCombo.Text = "" And Val(fctrWebCombo.Value) = 0 Then
            sql += " and planyear = " & Val(Session("PlanYearTxt"))
        Else
            sql += Session("SearchTxt")
        End If

        sql += " ORDER BY FundCenter_FundCenterCode "

        adapter.SearchFCTRcmmts(dtTable, sql)
        'Response.Write(sql)
        Session("count_data") = dtTable.Rows.Count
        'Session("SearchTxt") = SearchTxt.Text
        Session("fctrWebCombo") = fctrWebCombo.Value

        dtTable.Columns("PlanYear").AllowDBNull = True
        dtTable.Columns("FundCenter_FundCenterCode").AllowDBNull = True
        dtTable.Columns("FromCmmt_CmmtCode").AllowDBNull = True
        dtTable.Columns("ToCmmt_CmmtCode").AllowDBNull = True

        dtTable.Columns("Id").ReadOnly = False

        e.DataSource = dtTable
    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        ' e.Row.Cells(0).ForceNoEdit = True
        e.Row.Cells(4).ForceNoEdit = True
    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim adapter As New MasterDataSetTableAdapters.FundCenterCmmtsTableAdapter
        Dim check = adapter.CheckExistFCTRcmmts(Val(Session("PlanYearTxt")), e.Row.Cells(1).Value, Val(Session("PlanYearTxt")), e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), e.Row.Cells(3).Value)
        If check <> 0 Then
            WebGrid1.ClearCachedDataSource()
            'WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            'Dim code As String = ""
            ''MsgBox(Session("fctrWebCombo") & "/" & Session("SearchTxt") & "/" & L_CountData.Text)
            'If Session("fctrWebCombo") <> "" Then
            '    code = Session("fctrWebCombo")
            'ElseIf Session("SearchTxt") <> "" Then
            '    code = Session("SearchTxt")
            'ElseIf Session("SearchTxt") <> "" And Session("fctrWebCombo") <> "" Then
            '    code = Session("SearchTxt")
            'End If

            'code = Session("FctCode")

            'If code = "" Then
            '    WebGrid1.ClearCachedDataSource()
            '    Throw New ArgumentException("กรุณาระบุ Fund Center")
            'End If
            If e.Row.Cells(3).Value < e.Row.Cells(2).Value Then
                WebGrid1.ClearCachedDataSource()
                Throw New ArgumentException("ข้อมูลถึง Commitment น้อยกว่า จาก กรุณาตรวจสอบ")
            Else
                Dim id As Decimal
                id = adapter.InsertFCTRcmmt(Val(Session("PlanYearTxt")), Date.Now, Session("users"), e.Row.Cells(1).Value, Val(Session("PlanYearTxt")), e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), e.Row.Cells(3).Value, Val(Session("PlanYearTxt")))
                Dim dr As DataRow = e.Row.DataRow
                dr("id") = id

                logger.Info("User " & Session("users") & " insert commitment " & e.Row.Cells(2).Value & "-" & e.Row.Cells(3).Value & " for fund center " & e.Row.Cells(1).Value)
            End If
        End If
    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim id As String
        id = e.Row.KeyValue
        Dim adapter As New MasterDataSetTableAdapters.FundCenterCmmtsTableAdapter

        Dim check = adapter.CheckExistUpdate(Val(Session("PlanYearTxt")), e.Row.Cells(1).Value, Val(Session("PlanYearTxt")), e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), e.Row.Cells(3).Value, id)
        If check = 1 Then
            WebGrid1.ClearCachedDataSource()
            'WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูลซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        Else
            'Dim code As String = ""
            'MsgBox(Session("fctrWebCombo") & "/" & Session("SearchTxt") & "/" & L_CountData.Text)
            'If Session("fctrWebCombo") <> "" Then
            '    code = Session("fctrWebCombo")
            'ElseIf Session("SearchTxt") <> "" Then
            '    code = Session("SearchTxt")
            'ElseIf Session("SearchTxt") <> "" And Session("fctrWebCombo") <> "" Then
            '    code = Session("SearchTxt")
            'End If
            'code = Session("FctCode")
            If e.Row.Cells(3).Value < e.Row.Cells(2).Value Then
                WebGrid1.ClearCachedDataSource()
                Throw New ArgumentException("ข้อมูลถึง Commitment น้อยกว่า จาก กรุณาตรวจสอบ")
            Else
                adapter.UpdateFCTRcmmt(Val(Session("PlanYearTxt")), Date.Now, Session("users"), e.Row.Cells(1).Value, Val(Session("PlanYearTxt")), e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), e.Row.Cells(3).Value, Val(Session("PlanYearTxt")), id)
                logger.Info("User " & Session("users") & " insert commitment " & e.Row.Cells(2).Value & "-" & e.Row.Cells(3).Value & " for fund center " & e.Row.Cells(1).Value)
            End If
        End If
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim adapter As New MasterDataSetTableAdapters.FundCenterCmmtsTableAdapter
            ' MsgBox(Deletet)
            If type = "1" Then
                adapter.DeleteFCTRcmmt(Deletet)
                logger.Info("User " & Session("users") & " delete fund center-commitment " & Deletet)

            ElseIf type = "2" Then
                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                    adapter.DeleteFCTRcmmt(keyValue)
                    logger.Info("User " & Session("users") & " delete fund center-commitment " & keyValue)
                Next keyValue
            End If
            WebGrid1.ClientAction.Refresh()
        End If

    End Sub
    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click

        ' MsgBox(SearchTxt.Text)
        Session("PlanYearTxt") = PlanYearsTxt.Text
        Session("FctCode") = fctrWebCombo.Value
        If fctrWebCombo.Value <> "" And fctrWebCombo.Text <> "" And Val(fctrWebCombo.Value) <> 0 Then
            Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
            Session("SearchTxt") += " and FundCenter_FundCenterCode  = '" & fctrWebCombo.Value & "'"
            'WebGrid1.LayoutSettings.AllowAddNew = AddNew.Yes
            'Else
            '    'WebGrid1.LayoutSettings.AllowAddNew = AddNew.No
            '    Session("SearchTxt") = ""
            '    L_CountData.Text = "กรุณาเลือก Fund Center"
            '    Exit Sub
        End If

        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If
    End Sub

    Protected Sub FundCenterCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles FundCenterCombo.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        Dim dt As New BasicData.FCTR_ChildDataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " AND FundCenters.PlanYear = " & Val(Session("PlanYearTxt")) & " AND FundCenterHierarchies.PlanYear = " & Val(Session("PlanYearTxt"))
        sql += " AND FundCenterHierarchies.FundCenterGroup = 1 "
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += " ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)

        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt

    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub
    Sub Alert(ByVal message As String)
        AlertText = "<script> alert('" & message & "');</script>"
    End Sub

    Protected Sub fctrWebCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles fctrWebCombo.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        Dim dt As New BasicData.FCTR_ChildDataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " AND FundCenters.PlanYear = " & Val(Session("PlanYearTxt")) & " AND FundCenterHierarchies.PlanYear = " & Val(Session("PlanYearTxt"))
        sql += " AND FundCenterHierarchies.FundCenterGroup = 1 "
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += " ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)

        dt.Rows.Add("", "ทุกรายการ", 1)
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt
    End Sub
End Class
